#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using TVC.Layout;
using BUS;
#endregion

namespace TVC.Forms
{
    public partial class frmMME : Form
    {
        private DTO.PODInfo PODInfo;
        public MainForm MainForm { get; set; }
        string sErr = "";
        private string _Connecstr;
        public DataRow _ReturnValue;
        public DataTable dtItemCode;
        public DataTable dtLocaltion;
        public DataTable dtStatus;
        public DataTable dtmcbItemCode;

        public frmMME(GridLayout layout, MainForm form)
        {
            InitializeComponent();

            MainForm = form;
            PODInfo = (DTO.PODInfo)Session["USER"];
            _Connecstr = MainForm.HLConnect;

        }

        private void cbo_GotFocus(object sender, EventArgs e)
        {
            ComboBox cbo = (ComboBox)sender;
            if (cbo.Enabled == false)
                return;
            cbo.BackColor = System.Drawing.Color.Yellow;
            cbo.Focus();
        }

        private void cbo_LostFocus(object sender, EventArgs e)
        {
            ComboBox cbo = (ComboBox)sender;
            if (cbo.Enabled == false)
                return;
            cbo.BackColor = System.Drawing.Color.White;
        }

        private void dun_GotFocus(object sender, EventArgs e)
        {
            NumericUpDown dtp = (NumericUpDown)sender;
            if (dtp.Enabled == false || dtp.ReadOnly == true)
                return;
            dtp.BackColor = System.Drawing.Color.Yellow;
            dtp.Focus();
        }

        private void nud_LostFocus(object sender, EventArgs e)
        {
            NumericUpDown dtp = (NumericUpDown)sender;
            if (dtp.Enabled == false || dtp.ReadOnly == true)
                return;
            dtp.BackColor = System.Drawing.Color.White;
        }

        private void frmMME_Load(object sender, EventArgs e)
        {
            LoadCobobox();
            cboItemCode.Focus();
            if (_Mark == "-")
            {
                nudAmout.ReadOnly = true;
            }
            else
                nudAmout.ReadOnly = false;

            cboItemCode.SelectedValue = "";
            if (_info != null)
            {
                cboItemCode.SelectedValue = _info.ItemCode;
                SetData(_info);
                nudQuantity.Focus();
            }
        }


        private void LoadCobobox()
        {
            // Item Code            
            DataRow dr = dtItemCode.NewRow();
            dtItemCode.Rows.Add(dr);
            cboItemCode.DataSource = dtItemCode;
            cboItemCode.DisplayMember = "ItemCode";
            cboItemCode.ValueMember = "ItemCode";
            mcbItemCode.Binding = dtmcbItemCode;
            GridLayout layoutMovenentType = new GridLayout(MainForm.ServerPath
               + "Resources\\Layout\\", MainForm.ServerPath + "Documents\\Layout\\"
               + MainForm.PODInfo.USER_ID + "\\", "MovenentType");
            MainForm.InitMultiComboBox(mcbItemCode, layoutMovenentType, DTO.TVC_RFITMInfo.Field.ItemCode.ToString(), DTO.TVC_RFITMInfo.Field.Description.ToString());

            // Status

            cboStatus.DataSource = dtStatus;
            cboStatus.DisplayMember = "Code";
            cboStatus.ValueMember = "Code";
            // Localtion
            DataRow drLocaltion = dtLocaltion.NewRow();
            dtLocaltion.Rows.Add(drLocaltion);
            cboLocation.DataSource = dtLocaltion;
            cboLocation.DisplayMember = "Code";
            cboLocation.ValueMember = "Code";

            // cboBINnumber
            DataRow drBinNo = dtBinNo.NewRow();
            dtBinNo.Rows.Add(drBinNo);
            cboBinNumber.DataSource = dtBinNo;
            cboBinNumber.DisplayMember = "Code";
            cboBinNumber.ValueMember = "Code";

            //cboPartNo

            cboPartNo.DataSource = dtPartNo;
            cboPartNo.DisplayMember = "PartNo";
            cboPartNo.ValueMember = "PartNo";

            //cboAccountCode


            DataRow dr1 = dtacc1.NewRow();
            dr1["ACCNT_CODE"] = "";
            dr1["ACCNT_NAME"] = "";
            dr1["DisplayCBO"] = "";
            dtacc1.Rows.Add(dr1);
            cboAccount.DataSource = dtacc1;
            cboAccount.DisplayMember = "ACCNT_CODE";
            cboAccount.ValueMember = "ACCNT_CODE";

            //colSerialNo
            cboSerialNo.DataSource = dtSerialNo;
            cboSerialNo.DisplayMember = "SerialNo";
            cboSerialNo.ValueMember = "SerialNo";
        }

        private void cboItemCode_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cboItemCode.SelectedValue != null)
            {

                mcbItemCode.Text = cboItemCode.SelectedValue.ToString();
                lblMovementType.Text = ((DataTable)cboItemCode.DataSource).Rows[cboItemCode.SelectedIndex]["Description"].ToString().Trim();
                LoadValue(cboItemCode.SelectedValue.ToString());
            }
        }

        private void LoadValue(string ItemCode)
        {
            if (ItemCode != "System.Data.DataRowView")
            {
                BUS.TVC_MFMOVControl ctr = new TVC_MFMOVControl(PODInfo.Business_ID);
                DTO.TVC_MFMOVInfo info = new DTO.TVC_MFMOVInfo();
                info = ctr.GetbyItemCode(MainForm.SUNDB, ItemCode, ref sErr);
                info.Quantity = 0;
                info.Amount = 0;
                SetData(info);
            }
        }

        private void SetData(DTO.TVC_MFMOVInfo info)
        {
            cboPartNo.SelectedValue = info.PartNo;
            cboSerialNo.SelectedValue = info.SerialNo;
            cboStatus.SelectedValue = info.Status;
            cboLocation.SelectedValue = info.Location;
            cboBinNumber.SelectedValue = info.BINNo;
            cboAccount.SelectedValue = info.Account;
            nudAmout.Value = (decimal)info.Amount;
            nudQuantity.Value = info.Quantity;
        }

        private void mcbItemCode_ComboText_Changed(object sender, Modules.CustomControl.MultiColumnCombo.ComboChangedEventArgs e)
        {
            if (mcbItemCode.RowData != null)
                cboItemCode.SelectedValue = mcbItemCode.RowData["ItemCode"].ToString().Trim();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            GetData();
            DialogResult = DialogResult.OK;
            Close();
        }

        private void GetData()
        {
            _ReturnValue["ItemCode"] = cboItemCode.SelectedValue.ToString();
            _ReturnValue["BINNo"] = cboBinNumber.SelectedValue == null ? "" : cboBinNumber.SelectedValue.ToString();
            _ReturnValue["Account"] = cboAccount.SelectedValue == null ? "" : cboAccount.SelectedValue.ToString();
            _ReturnValue["Location"] = cboLocation.SelectedValue == null ? "" : cboLocation.SelectedValue.ToString();
            _ReturnValue["PartNo"] = cboPartNo.SelectedValue == null ? "" : cboPartNo.SelectedValue.ToString();
            _ReturnValue["SerialNo"] = cboSerialNo.SelectedValue == null ? "" : cboSerialNo.SelectedValue.ToString();
            _ReturnValue["Status"] = cboStatus.SelectedValue == null ? "" : cboStatus.SelectedValue.ToString();
            _ReturnValue["Amount"] = nudAmout.Value.ToString();
            _ReturnValue["Quantity"] = nudQuantity.Value.ToString();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void frmMME_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            //if (!cboItemCode.Focus() && !cboLocation.Focus() && !cboPartNo.Focus() && !cboSerialNo.Focus()
            //    && !cboStatus.Focus() && !cboAccount.Focus() && !cboBinNumber.Focus())
            //{
            //    if (objArgs.KeyCode == Keys.Escape)
            //    {
            //        btnCancel_Click(null, null);
            //    }
            //}
        }




        public DataTable dtBinNo { get; set; }

        public DataTable dtPartNo { get; set; }

        public DataTable dtacc1 { get; set; }

        public DataTable dtSerialNo { get; set; }

        public string _Mark { get; set; }

        public DTO.TVC_MFMOVInfo _info { get; set; }
    }
}